<?php


namespace app\index\controller;


use app\admin\model\Navigation;
use app\admin\model\NewsCate;
use app\admin\model\News as NewsModel;

class News extends Base
{
    public function index()
    {
        $cate = NewsCate::where(['status'=>1])->limit(0,3)->order('sort asc')->select();
        $cate_id = input('cate_id',1);
        $type = $cate_id;
        $news = NewsModel::where(['status'=>1,'cate_id'=>$cate_id])->order('sort asc')->paginate(2,false);
        $page = $news->render();
        return $this->view->fetch('',[
            'cate' => $cate,
            'news' => $news,
            'page' => $page,
            'type' => $type,
        ]);
    }

    public function details()
    {
        $cate = NewsCate::where(['status'=>1])->limit(0,5)->order('sort asc')->select();
        $cate_id = input('cate_id',1);
        $cate_name = NewsCate::find($cate_id);
        $type = $cate_id;
        $id = input('id');
        $news = NewsModel::find($id);
        $news->save(['num'=>$news['num']+1]);
        $upper = NewsModel::where('id', '<', $id)
            ->where('status', 1)
            ->order('id desc')
            ->find();   //上一条
        $lower = NewsModel::where('id', '>', $id)
            ->where('status', 1)
            ->order('id asc')
            ->find();   //下一条
        return $this->view->fetch('',[
            'cate' => $cate,
            'cate_name' => $cate_name,
            'news' => $news,
            'type' => $type,
            'cate_id' => $cate_id,
            'upper' => $upper,
            'lower' => $lower,
        ]);
    }
}